﻿Module ModuleTrack
	Sub GenerateListView(ByRef tracks() As Track, ByRef listview As ListView)
		listview.Items.Clear()
		listview.Columns.Clear()
		listview.Groups.Clear()
		listview.Columns.Add("#")
		listview.Columns.Add("name")
		listview.Columns.Add("Duration")
		listview.Groups.Add("Tracks", "Tracks")

		listview.Columns.Item(0).Width = 30
		listview.Columns.Item(1).Width = 150

		For Each track In tracks
			Dim item = New ListViewItem(Integer.Parse(track.rank), listview.Groups(0))
			item.SubItems.Add(track.name)

			Dim duration As Integer = Integer.Parse(track.duration)
			item.SubItems.Add((duration \ 60).ToString + ":" + (duration Mod 60).ToString("d2"))

			listview.Items.Add(item)
		Next
	End Sub

	Sub AddToListView(ByRef tracks() As Track, ByRef listview As ListView)
		If IsNothing(tracks) Then
			Return
		End If

		Dim art As New PictureBox

		For Each track As Track In tracks
			Dim item As ListViewItem = GenerateListViewItemSearch(track.name, track.artist, track.name, track.mbid, 0, listview.Groups("Tracks"))
			If Not (IsNothing(track.image)) Then
				If track.image(0) <> Nothing Then
					art.Load(track.image(0))
					listview.SmallImageList.Images.Add(art.Image)
					item.ImageIndex = listview.SmallImageList.Images.Count - 1
				End If
			End If

			listview.Items.Add(item)
		Next

		listview.LargeImageList = listview.SmallImageList
		art.Dispose()
	End Sub
End Module
